﻿Office.initialize = function (reason) {
    $(document).ready(function () {
        AGSamples.translator.initialize();
    });
};

var AGSamples = window.AGSamples || {};

AGSamples.translator = {
    languageFrom: "nl",
    languageTo: "en",
    selectedText: "",

    initialize: function () {
        $('#btnTranslate').click(Function.createDelegate(this, this.btnTranslate_click));
    },

    btnTranslate_click: function () {
        this.languageFrom = $("#lstFrom").val();
        this.languageTo = $("#lstTo").val();

        Office.context.document.getSelectedDataAsync(
            Office.CoercionType.Text,
            Function.createDelegate(this, this.getSelectedData_returned));
    },

    getSelectedData_returned: function (result) {
        if (result.status == "succeeded") {
            this.selectedText =result.value;
            this.translateSelectedText();
        }
    },
    translateSelectedText: function () {
        //ERROR:alert(this.selectedText);

        //http://www.bing.com/toolbox/bingsearchapi
        //https://datamarket.azure.com/account/datasets
        //https://datamarket.azure.com/dataset/bing/microsofttranslator
        //http://blogs.msdn.com/b/translation/p/gettingstarted1.aspx
        
        var scriptElement = document.createElement("script");
        scriptElement.src = 'http://api.microsofttranslator.com/V2/Ajax.svc/Translate';
        scriptElement.src += '?oncomplete=AGSamples.translator.getTranslation';
        scriptElement.src += '&appId=Bearer ' + encodeURIComponent(window.accessToken);
        scriptElement.src += '&text=' + encodeURIComponent(this.selectedText);
        scriptElement.src += '&from=' + this.languageFrom + '&to=' + this.languageTo;
        document.getElementsByTagName('head')[0].appendChild(scriptElement);
    },

    getTranslation: function (result) {
        $("#results").text(result);
    }



}



function MicrosoftTranslateStart(text, language) {
    
}
function MicrosoftTranslateComplete(result) {
    alert(result);
}





